package cn.wjdiankong.levideo.activity;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.dialog.ClearCacheDialog;
import cn.wjdiankong.levideo.dialog.ClearCacheDialog.ClickClearListener;
import cn.wjdiankong.levideo.dialog.DragTabSortDialog;
import cn.wjdiankong.levideo.utils.AdUtils;
import cn.wjdiankong.levideo.utils.AdUtils.AdListener;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.utils.FileUtils;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.UmengReportUtils;
import cn.wjdiankong.levideo.utils.Utils;
import net.youmi.android.nm.vdo.VideoAdManager;

public class SettingActivity extends BaseActivity implements OnClickListener{

	private int douyinCacheSize, huoshanCacheSize, kuaishouCacheSize, miaopaiCacheSize, toutiaoCacheSize, imgCacheSize, totalCacheSize;
	private TextView cacheTxt;

	/**
	 * 原生视频广告控件容器
	 */
	private RelativeLayout mNativeVideoAdLayout;
	
	/**
	 * 视频信息栏容器
	 */
	private RelativeLayout mVideoInfoLayout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setting);
		initView();
		initData();
		isGestureFinish = true;
	}

	public void initView(){
		super.initView();

		cacheTxt = (TextView) findViewById(R.id.cache_txt);
		cacheTxt.setText("正在计算缓存数据...");

		// 原生视频广告控件容器
		mNativeVideoAdLayout = (RelativeLayout) findViewById(R.id.rl_native_video_ad);
		// 视频信息栏容器
		mVideoInfoLayout = (RelativeLayout) findViewById(R.id.rl_video_info);

		findViewById(R.id.layout_aboutme).setOnClickListener(this);
		findViewById(R.id.layout_toutiao_video).setOnClickListener(this);
		findViewById(R.id.layout_localvideo).setOnClickListener(this);
		findViewById(R.id.layout_aboutapp).setOnClickListener(this);
		findViewById(R.id.layout_clearcache).setOnClickListener(this);
		findViewById(R.id.layout_drag_sort).setOnClickListener(this);
		findViewById(R.id.layout_book).setOnClickListener(this);
		findViewById(R.id.layout_supportapp).setOnClickListener(this);

	}

	protected void initData(){
		super.initData();
		setTitle("设置");
		setLeftImg(R.drawable.back_icon);
		new AsyncTask<Void, Void, Void>(){
			@Override
			protected Void doInBackground(Void... params) {
				douyinCacheSize = FileUtils.getVideoTypeCacheSize(Const.VIDEO_TYPE_DOUYIN);
				huoshanCacheSize = FileUtils.getVideoTypeCacheSize(Const.VIDEO_TYPE_HUOSHAN);
				miaopaiCacheSize = FileUtils.getVideoTypeCacheSize(Const.VIDEO_TYPE_MIAOPAI);
				kuaishouCacheSize = FileUtils.getVideoTypeCacheSize(Const.VIDEO_TYPE_KUAISHOU);
				toutiaoCacheSize = FileUtils.getVideoTypeCacheSize(Const.VIDEO_TYPE_TOUTIAO);
				imgCacheSize = FileUtils.getImgCacheSize();
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				totalCacheSize = douyinCacheSize+huoshanCacheSize+miaopaiCacheSize+kuaishouCacheSize+toutiaoCacheSize+imgCacheSize;
				if(totalCacheSize <= 0){
					cacheTxt.setText("暂时缓存数据");
				}else{
					cacheTxt.setText(Utils.formatSize(totalCacheSize));
				}
			}

		}.execute();

		VideoAdManager.getInstance(getApplicationContext()).setUserId("fourbrother");
		
		setupNativeVideoAd();

	}

	@SuppressLint("InflateParams")
	private void showDragTabSortDialog(){
		DragTabSortDialog dialog = new DragTabSortDialog(this);
		dialog.show();
	}

	@SuppressLint("InflateParams")
	private void showCacheDialog(){
		ClearCacheDialog dialog = new ClearCacheDialog(this);
		dialog.setDouyinCacheSize(douyinCacheSize)
		.setHuoshanCacheSize(huoshanCacheSize)
		.setKuaishouCacheSize(kuaishouCacheSize)
		.setMiaopaiCacheSize(miaopaiCacheSize)
		.setToutiaoCacheSize(toutiaoCacheSize)
		.setImageCacheSize(imgCacheSize)
		.setClickClearListener(new ClickClearListener() {
			@Override
			public void clear(int type, int cacheSize) {
				totalCacheSize -= cacheSize;
				if(totalCacheSize <= 0){
					totalCacheSize = 0;
				}
				cacheTxt.setText(Utils.formatSize(totalCacheSize));

				String videoTypeStr = Utils.getVideoTypeStr(type);
				HashMap<String, String> params = new HashMap<String, String>();
				params.put("video_source", videoTypeStr);
				UmengReportUtils.report(UmengReportUtils.SETTING_CLEAR_CACHE, params);

			}
		});
		dialog.show();
	}

	public static void startDefault(Context context){
		Intent intent = new Intent(context, SettingActivity.class);
		context.startActivity(intent);
	}

	public static void startDefaultResult(Activity act, int code){
		Intent intent = new Intent(act, SettingActivity.class);
		act.startActivityForResult(intent, code);
	}

	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) {  
		if(keyCode == KeyEvent.KEYCODE_BACK)  {
			setResult(MainActivity.CODE_SETTING_SORT_TAB_REULT);
			finish();
		}  
		return false;  
	} 

	@Override
	protected void clickLeftImg() {
		super.clickLeftImg();
		finish();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.layout_aboutme:
			AboutMeActivity.startDefault(this);
			UmengReportUtils.report(UmengReportUtils.SETTING_ABOUT_ME);
			break;
		case R.id.layout_aboutapp:
			UmengReportUtils.report(UmengReportUtils.SETTING_LAW);
			AppLawActivity.startDefault(this);
			break;
		case R.id.layout_clearcache:
			UmengReportUtils.report(UmengReportUtils.SETTING_CLEAR_CACHE);
			if(totalCacheSize <= 0){
				return;
			}
			showCacheDialog();
			break;
		case R.id.layout_drag_sort:
			UmengReportUtils.report(UmengReportUtils.SETTING_TABSORT);
			showDragTabSortDialog();
			break;
		case R.id.layout_localvideo:
			UmengReportUtils.report(UmengReportUtils.SETTING_LOCALVIDEO);
			LocalVideoActivity.startDefault(this);
			break;
		case R.id.layout_toutiao_video:
			UmengReportUtils.report(UmengReportUtils.SETTING_TOUTIAO);
			ToutiaoVideoActivity.startDefault(this);
			break;
		case R.id.layout_book:
			UmengReportUtils.report(UmengReportUtils.SETTING_BOOK);
			BookActivity.startDefault(this);
			break;
		case R.id.layout_supportapp:
			UmengReportUtils.report(UmengReportUtils.SETTING_SUPPORT_APP);
			SupportAppActivity.startDefault(this);
			break;
		}
	}

	@Override
	protected String getPageName() {
		return "setting";
	}

	@Override
	protected void onStart() {
		super.onStart();
		// 原生视频广告
		VideoAdManager.getInstance(getApplicationContext()).onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 原生视频广告
		VideoAdManager.getInstance(getApplicationContext()).onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 原生视频广告
		VideoAdManager.getInstance(getApplicationContext()).onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
		// 原生视频广告
		VideoAdManager.getInstance(getApplicationContext()).onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 原生视频广告
		VideoAdManager.getInstance(getApplicationContext()).onDestroy();
	}

	/**
	 * 设置原生视频广告
	 */
	public void setupNativeVideoAd() {
		Logger.log("get video ad...");
		AdUtils.showVideoAd(mNativeVideoAdLayout, mVideoInfoLayout, new AdListener() {
			
			@Override
			public void showSucc() {
			}
			
			@Override
			public void showFail() {
				Logger.log("video ad show err");
			}
			
			@Override
			public void close() {
				
			}
			
			@Override
			public void click() {
				
			}
		});
	}

}
